package com.mvvm.store.ui.delivery;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.KeyEvent;
import android.view.View;

import com.mvvm.store.R;
import com.mvvm.store.base.BaseActivity;
import com.mvvm.store.base.NormalViewModel;
import com.mvvm.store.bean.DeliveryParasBean;
import com.mvvm.store.bean.StockUpParasBean;
import com.mvvm.store.bean.basebean.DeliveryInfoBean;
import com.mvvm.store.bean.basebean.DeliveryResultBean;
import com.mvvm.store.bean.basebean.ParamsBuilder;
import com.mvvm.store.bean.basebean.Rows;
import com.mvvm.store.bean.basebean.StockUpResultBean;
import com.mvvm.store.customview.CustomProgress;
import com.mvvm.store.databinding.ActivityOutBoundManagerBinding;
import com.mvvm.store.ui.delivery.adapter.OutBoundManagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class OutboundManagerActivity extends BaseActivity<DeliveryViewModel, ActivityOutBoundManagerBinding> {
    int index;
    private OutBoundManagerAdapter outBoundManagerAdapter;
    private int curPage = 0;
    private ArrayList<Rows> rowsArrayList = new ArrayList<>();
    private CustomProgress dialog;
    private   DeliveryParasBean deliveryParasBean;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_out_bound_manager;
    }

    @Override
    protected void processLogic() {
        binding.leoTitleBar.bar_left_btn.setBackgroundResource(R.mipmap.return_images);
        if(dialog==null) {
            dialog = CustomProgress.show(this, "", true, null);
        }
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    Thread.sleep(500);//休眠3秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                /**
                 * 要执行的操作
                 */
                dialog.dismiss();
            }
        }.start();
        binding.includeEmpty.setTextEmpty("瞅啥，没有你的出库数据~");
        binding.includeEmpty.setImageEmpty(R.mipmap.no_data);
        //获取接口返回的数据
        //入参
         deliveryParasBean=new DeliveryParasBean();
        deliveryParasBean.setOutStockOrderNo("");
        deliveryParasBean.setPageNum(0);
        deliveryParasBean.setPageSize(0);
        String authorization="";
        getDeliveryList(authorization,deliveryParasBean,null);
        outBoundManagerAdapter=new OutBoundManagerAdapter();
        outBoundManagerAdapter.setDataList(rowsArrayList);
        binding.recyclerView.setAdapter(outBoundManagerAdapter);

        outBoundManagerAdapter.setOnclick(new OutBoundManagerAdapter.ClickInterface() {

            @Override
            public void onImagesClick(View view, int position, Rows rowsListBean) {
                System.out.println("deliveryResultBeasssssssssssssn-----------"+rowsListBean.toString());
                SharedPreferences sharedPreferences = getSharedPreferences("deliveryInfo", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("id", rowsListBean.getId()+"");
                editor.putString("stockOrderNo", rowsListBean.getOutStockOrderNo()+"");
                editor.commit();
                Intent intent=new Intent(OutboundManagerActivity.this,DeliveryManagerActivity.class);
                startActivity(intent);
            }

            @Override
            public void onItemClick(View view, int position, Rows deliveryResultBean) {

            }
        });

    }

    @Override
    protected void setListener() {
        binding.leoTitleBar.bar_left_btn.setOnClickListener(this::onClick);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bar_left_btn:
                this.finish();
                break;
            default:
                break;
        }
    }

    private void getDeliveryList(String authorization,DeliveryParasBean deliveryParasBean, ParamsBuilder paramsBuilder){
        mViewModel.getDeliveryList(deliveryParasBean,paramsBuilder).observe(this,resourse->resourse.handler(new OnCallback<DeliveryResultBean>() {
            @Override
            public void onSuccess(DeliveryResultBean data) {
             List<Rows> rowsList=  data.rows;
            if(data.rows.size()>0){
                binding.includeEmpty.relativeNull.setVisibility(View.GONE);
                for (int i=0;i<rowsList.size();i++){
                    rowsArrayList.add(rowsList.get(i));
                }

            }else {
                binding.includeEmpty.relativeNull.setVisibility(View.VISIBLE);
            }
                outBoundManagerAdapter.notifyDataSetChanged();
                System.out.println("获取数据"+data.toString());

            }
        }));
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        System.out.println("33333");
        rowsArrayList.clear();
        getDeliveryList("",deliveryParasBean, null);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
